package GateWars;

import List.PlayerNode;
import List.SortedList;

public class BFParser {
	
	public static void parse(String s) {
		String[] elements = s.split("<TR>");
		/*for(int i = 0; i < elements.length; i++){
			System.out.print(i + ":\n" + elements[i]);
		}*/
		String[] temp;
		temp = elements[13].split("Turns: ");
		temp = temp[1].split("</b>");
		GateWarsFactory.getGateWarsManager().turns = new Short(temp[0].trim().replace(",", ""));
		
		temp = elements[14].split("Naquadah: ");
		temp = temp[1].split("</b>");
		GateWarsFactory.getGateWarsManager().naq = new Long(temp[0].trim().replace(",", ""));
		
		temp = elements[15].split(": ");
		temp = temp[3].split("</A>");
		GateWarsFactory.getGateWarsManager().bankNaq = new Long(temp[0].trim().replace(",", ""));
		
		temp = elements[16].split("Next Turn:");
		temp = temp[1].split(" min");
		GateWarsFactory.getGateWarsManager().nextTurn = new Byte(temp[0].trim());
		
		int offset = 0;
		if(elements[24].contains("rank_my_peace"))
		{
			offset = 2;
		}
		int id, army, relation;
		String alliance, name, race, ascention;
		long naq;
		SortedList currentPage = new SortedList();
		if(currentPage.getLargestArmy() != null || currentPage.getLargestNaq() != null)
			System.out.println("hmm?");
		for(int i = 24 + offset; i <= (elements.length - (5 - (offset/2))); i+=4){
			id = 0;
			army = 0;
			alliance = "";
			name = "";
			race = "";
			ascention = "";
			naq = 0;
			relation = 0;
			if(elements[i].indexOf("id=") != -1)
			{
				temp = elements[i].split("id=");
				temp = temp[1].split("\">");
				if(temp.length > 2 && temp[2].startsWith("<i>"))
				{
					String rString = temp[2].substring(5, temp[2].indexOf(" </"));
					if(rString.equals("war")){
						relation = 1;
					}
					relation = 2;
				}
				id = new Integer(temp[0].trim());
				name = temp[1].split("</A>")[0].trim();
			}
			else
			{
				temp = elements[i].split("<TD>");	
				temp = temp[1].split("<TD colspan");
				name = temp[0].trim();
			}
			
			temp = elements[i + 1].split("</TABLE>");
			if(temp[0].indexOf("color=red>") != -1)
			{
				ascention = temp[0].split("color=red>")[1].split("</b>")[0].trim();
			}
			if(temp[0].indexOf("[") != -1)
			{
				if(temp[0].indexOf("-") != -1)
				{
					alliance = temp[0].substring(temp[0].indexOf("[") + 2).split("<i>-")[0].trim();
				}
				else
				{
					String a = temp[0].substring(temp[0].indexOf("[") + 2);
					alliance = a.substring(0, a.indexOf("]")).trim();
				}
			}
			
			temp = elements[i + 1].split("white\">");
			if(temp[2].split("</font>")[0].trim().replace(",", "").charAt(0) != '?')
				army = new Integer(temp[2].split("</font>")[0].trim().replace(",", ""));
			race = temp[3].split("</font>")[0].trim();
			if(temp[4].split("</font>")[0].split("Naq")[0].replace(",", "").trim().charAt(0) != '?')
				naq = new Long(temp[4].split("</font>")[0].split("Naq")[0].replace(",", "").trim());
			
			//System.out.println(name + " : " + id + " : " + army + " : " + alliance + " : " + naq + " : " + race + " : " + ascention + " : " + relation);

			PlayerNode tempPlayer = new PlayerNode(name, army);
			if(id != 0)
				tempPlayer.setId(id);
			if(!alliance.equals(""))
				tempPlayer.setAlliance(alliance);
			if(!ascention.equals(""))
				tempPlayer.setAscention(ascention);
			tempPlayer.setAsgard(race.equals("Asgard"));
			tempPlayer.setNaq(naq);
			tempPlayer.setRelation(relation);
			//GateWarsFactory.addPlayer(tempPlayer);
			tempPlayer = new PlayerNode(name, army);
			if(id != 0)
				tempPlayer.setId(id);
			if(!alliance.equals(""))
				tempPlayer.setAlliance(alliance);
			if(!ascention.equals(""))
				tempPlayer.setAscention(ascention);
			tempPlayer.setAsgard(race.equals("Asgard"));
			tempPlayer.setNaq(naq);
			tempPlayer.setRelation(relation);
			currentPage.addPlayer(tempPlayer);
		}
		try{
			if(elements[elements.length - 4 + (int)(offset*1.5)].split("of")[0].split("page").length > 2)
				GateWarsFactory.getGateWarsManager().fParsingBFPage(new Integer(elements[elements.length - 4 + (int)(offset*1.5)].split("of")[0].split("page")[2].trim()), currentPage);
			else
				GateWarsFactory.getGateWarsManager().fParsingBFPage(new Integer(elements[elements.length - 4 + (int)(offset*1.5)].split("of")[0].split("page")[1].trim()), currentPage);
		}
		catch (ArrayIndexOutOfBoundsException e){}
	}

}
